//////////////////////////////////////////////////////////////////
// By Team SquallSoft. 2011                                     //
// psphase.ismywebsite.com                                      //
// Project: Phase Changing                                      //
// Build: 224                                                   //
// Stage: Alpha                                                 //
// Revision: 4                                                  //
// License: GPL v3                                              //
//////////////////////////////////////////////////////////////////


//including libraries

#include "headers.h"

PSP_MODULE_INFO("Phase Changing", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | PSP_THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(12*1024);


//Exit Callback
int LoadImages();
int BeginStory();

int exit_callback(int arg1, int arg2, void *common)
{
    osl_quit = 1;
    return 0;
}



//Setting Images in OSL//
// notice "map" is a useless image that isnt created. But its there to fix my map switching problem
OSL_IMAGE *map,*menui, *rip, *win;
OSL_IMAGE *play, *play2, *load, *load2, *credits, *credits2, *exiti, *exiti2, *menubg;
OSL_IMAGE *player, *dragon, *map2, *bar, *choose;
OSL_SOUND *music;

//Initalizing OSlib//


int initOSLib()
{
    oslInit(0);
    oslInitGfx(OSL_PF_8888, 1);
    oslInitAudio();
    oslSetQuitOnLoadFailure(1);
    oslSetKeyAutorepeatInit(40);
    oslSetKeyAutorepeatInterval(10);
    return 0;
}

// Ending OSlib//


int endOSLib()
{
    oslEndGfx();
    osl_quit = 1;
    return 0;
}

//Variables//

int screen = 0;
int menu = 0;
int hp = 40000;
int mp = 100;
int score = 0;
int money = 0;
int sec = 100;
int sec2 = 100;
int story = 0;
int zoom = 0;
int sec3 = 100;
int intro = 1;
int attax = 0;
int damage = 0;
int enemyattack = 100;
int enemyhp = 40000;

//int mapset = 0;


// Stuffff....//

int MenuControl()
{
            if (osl_keys->held.start)
			{
			screen = 4;
			}
			
			if (osl_keys->pressed.select)
			{
			screen = 0;
			}
			return 0;
}

int BasicWall()
{
	if (player->x < 0){
	player->x = 0;
	}
	if (player->y < 14){
	player->y = 14;
	}
	if (player->x > 448){
    screen = 5;
	intro = 2;
	}
	if (player->y > 136){
	player->y = 136;
	}
	return 0;
	
}

int BasicWall2()
{
	if (player->x < -88){
	screen = 1;
	intro = 0;
	}
	if (player->y < -20){
	player->y = -20;
	}
	if (player->x > 448){
    player->x = 448;
	}
	if (player->y > 136){
	player->y = 136;
	}
	return 0;
	
}

int AttackEnemy(){

if (choose->x == 0) attax = 0;
if (choose->x == 45) attax = 1;
if (attax == 0) damage = 90;
if (attax == 1) damage = 130;

enemyhp = enemyhp-damage; 
hp = hp-enemyattack;

return 0;

}

////////////////////////////////////////////////////////////////////////////////////////////
// END OF INT
////////////////////////////////////////////////////////////////////////////////////////////



int main()
{
    int skip = 0;
    initOSLib();
	oslIntraFontInit(INTRAFONT_CACHE_MED);
	
	LoadImages();
	
	OSL_FONT* font;
    font = oslLoadFontFile("./Res/Font/font.pgf");
    oslIntraFontSetStyle(font, 1.0f,RGBA(255,255,255,255), RGBA(0,0,0,0),INTRAFONT_ALIGN_LEFT);
    oslSetFont(font);
	

    while(!osl_quit)
    {
        if (!skip)
        {
            oslStartDrawing();
			oslCls();
			
			oslReadKeys();
			
			
		if(screen == 0){
		            if(intro == 1) {oslShowSplashScreen(1); intro = 0; oslSetSoundLoop(music, 1); oslPlaySound(music, 0);}
		            oslDrawImage(menubg);
		            oslPrintf_xy(15,8,"0.4.1");
					oslDrawImageXY(play2, 75, 180 );
					oslIntraFontSetStyle(font, 1.5f,RGBA(188,188,188,255), RGBA(0,0,0,0),INTRAFONT_ALIGN_CENTER);
			        (menu == 0) ? oslDrawString(220,125,"Single Player") : oslDrawImageXY(play, 75, 180 );
					oslDrawImageXY(load2, 175, 180 );
					(menu == 1) ? oslDrawString(220,125,"MultiPlayer") : oslDrawImageXY(load, 175, 180 );
					oslDrawImageXY(credits2, 275, 180 );
					(menu == 2) ? oslDrawString(220,125,"About") : oslDrawImageXY(credits, 275, 180 );
					oslDrawImageXY(exiti2, 375, 180 );
					(menu == 3) ? oslDrawString(220,125,"Exit") : oslDrawImageXY(exiti, 375, 180 );
						
					if(osl_keys->pressed.left && menu > 0) menu --; 
					if(osl_keys->pressed.right && menu < 3) menu ++; 
					if(osl_keys->pressed.cross){
						if(menu == 0){
							screen = 1;
						}
						if(menu == 1){
							screen = 2;
						}
						if(menu == 2){
							screen = 3;
							}
                        if(menu == 3){
						 oslQuit();
						}
						
						
					
					}
					}
			if(screen == 1){
				BeginStory();
				if (osl_keys -> pressed.circle){
				    story = 1;
					screen = 1;
			     } 
			}
			
			
			if(screen == 2){
			oslIntraFontSetStyle(font, 1.0f,RGBA(188,188,188,255), RGBA(0,0,0,0),INTRAFONT_ALIGN_CENTER);
            oslDrawString(80,20,"Isnt possible Yet!");
			oslDrawString(80,40,"Press 0 to go back");
			if(osl_keys->pressed.circle){
			screen = 0;
			}
			}
			
			
			
			if(screen ==3){
			oslIntraFontSetStyle(font, 1.0f,RGBA(188,188,188,255), RGBA(0,0,0,0),INTRAFONT_ALIGN_CENTER);
			oslDrawString(240,55,"About");
			oslDrawString(250,100,"");
			oslDrawString(250,115,"music by hamstercake");
			oslDrawString(250,130,"Game made by SquallSoft");
			oslDrawString(250,145,"DEMO VERSION");
			oslDrawString(250,160,"");
			oslDrawString(345,250,"Press 0 to go back");
			if (osl_keys -> pressed.circle){
			screen = 0;
			}
			}
			
			
			if (screen == 4){
			oslDrawImage(menui);
			oslPrintf_xy(45,40,"HP: %i", hp);
			oslPrintf_xy(45,50,"MP: %i", mp);
			oslPrintf_xy(45,60,"Score: %i", score);
			oslPrintf_xy(45,70,"Money: %i$", money);
			oslPrintf_xy(45,90,"SpriteX: %i", player->x);
			oslPrintf_xy(45,110,"SpriteY: %i", player->y);
			
			if (osl_keys->released.start){
			screen = 5;
			}
            }
			

			if (screen == 1 && story == 1){
			oslDrawImage(map);
			oslDrawImage(player);
			oslDrawImage(bar);
			oslDrawImage(choose);
			oslPrintf_xy(0,0,"HP: %i", hp);
			oslPrintf_xy(0,12,"MP: %i", mp);
			if (osl_keys->pressed.R) choose->x +=45; 
			if (osl_keys->pressed.L) choose->x -=45;
			if (osl_keys->held.down) player->y += 2;
		    if (osl_keys->held.up) player->y -= 2;
		    if (osl_keys->held.left) player->x -= 2;
		    if (osl_keys->held.right) player->x += 2;
			if (intro == 0){
			player->x = 240;							
	        player->y = 136;
			intro = 2;
			}
			BasicWall();
			MenuControl();

		    }
			
			if (screen == 5){
			oslDrawImage(map2);
			oslDrawImage(player);
			oslDrawImage(dragon);
			oslDrawImage(bar);
			oslDrawImage(choose);
			oslPrintf_xy(0,0,"HP: %i", hp);
			oslPrintf_xy(0,12,"MP: %i", mp);
			oslPrintf_xy(200,0,"HP enemy: %i",enemyhp);
			if (osl_keys->pressed.R) choose->x +=45; 
			if (osl_keys->pressed.L) choose->x -=45;
			if (osl_keys->held.down) player->y += 2;
		    if (osl_keys->held.up) player->y -= 2;
		    if (osl_keys->held.left) player->x -= 2;
		    if (osl_keys->held.right) player->x += 2;
			if (osl_keys->pressed.cross) AttackEnemy();
			if (hp < 0) oslDrawImage(rip);
			if (enemyhp < 0) oslDrawImage(win);
			if (intro == 2){
			player->x = 20;							
	        player->y = 90;
			dragon->x = 190;
			dragon->y = 40;
			intro = 5;
			}
			BasicWall2();
			MenuControl();
			
			
			}

			
					
            oslEndDrawing();
        }


        
        oslEndFrame();
		oslAudioVSync();
    	skip = oslSyncFrame();
    }
	
    oslDeleteFont(font);
	oslIntraFontShutdown();
    endOSLib();
    oslQuit();
    return 0;
}

